package com.hc.cesium.controller;

import com.hc.cesium.mapper.UserMapper;
import com.hc.cesium.model.User;
import com.hc.cesium.service.UserService;
import com.hc.cesium.util.CodeMsg;
import com.hc.cesium.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<User>> test(){
        User user = new User();
        List<User> all = userService.findAll(user);
        return Result.success(all);
    }
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ResponseBody
    public Result<String> login(User user, HttpSession session){
        User user1 = userService.findByName(user.getName());
        if (user1!=null&&user1.getPassword().equals(user.getPassword())){
            session.setAttribute("user",user1);
            return Result.success("登陆成功");
        }
        return Result.error(CodeMsg.BIND_ERROR);
    }
}
